﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Newtonsoft.Json;
using Pay.Model.Model;

namespace Pay.Admin.App_Start
{
    public class MyAuthorizeAttribute: AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            var attr = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AllowAnonymousAttribute), true);
            if (attr.Any())
            {
                return;
            }
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
               
                var cookie = filterContext.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                var Current = JsonConvert.DeserializeObject<UserInfo>(ticket.UserData);
                filterContext.RouteData.Values["UserInfo"] = Current;
                return;
            }
            else
            {
                filterContext.Result = new RedirectResult("/user/login");
            }
        }
    }
}